
global path "YOUR_PATH\Replication_Code"

*** ----------------------------------------------------------------------------
*** Table 1 - consensus --------------------------------------------------------
*** ----------------------------------------------------------------------------

use "$path\02_OutData\forecasts_act_ltg_cons.dta", clear 

*Change the structure for prior-consistent regs
expand 2 if !missing(revision_good_mean1) & !missing(revision_bad_mean1), gen(dup)
replace revision_good_mean1=. if !missing(revision_good_mean1) & !missing(revision_bad_mean1) & dup==0
replace revision_bad_mean1=. if !missing(revision_good_mean1) & !missing(revision_bad_mean1) & dup==1
forval k=3/5{
	replace forecast_error`k'_good_mean1=. if !missing(revision_good_mean1) & !missing(revision_bad_mean1) & dup==0
	replace forecast_error`k'_bad_mean1=. if !missing(revision_good_mean1) & !missing(revision_bad_mean1) & dup==1
}
*Prior-consistent 
forval k=3/5{
	gen fe`k'=forecast_error`k'_good_mean1
	replace fe`k'=forecast_error`k'_bad_mean1 if missing(fe`k')
}

gen revision_mean=revision_good_mean1
replace revision_mean=revision_bad_mean1 if missing(revision_mean)

gen pct_bp4 = .
replace pct_bp4 = 1 if (revision_good_mean1>0 & revision_good_mean1<=200 & !missing(fe4))
replace pct_bp4 = 0 if (revision_good_mean1<0 & revision_good_mean1>=-200 & !missing(fe4)) | (revision_bad_mean1>0 & revision_bad_mean1<=200 & !missing(fe4)) | (revision_bad_mean1<0 & revision_bad_mean1>=-200 & !missing(fe4))

gen pct_sn4 = .
replace pct_sn4 = 1 if (revision_bad_mean1<0 & revision_bad_mean1>=-200 & !missing(fe4))
replace pct_sn4 = 0 if (revision_good_mean1<0 & revision_good_mean1>=-200 & !missing(fe4)) | (revision_bad_mean1>0 & revision_bad_mean1<=200 & !missing(fe4)) | (revision_good_mean1>0 & revision_good_mean1<=200 & !missing(fe4))

gen pct_sp4 = .
replace pct_sp4 = 1 if (revision_bad_mean1>0 & revision_bad_mean1<=200 & !missing(fe4))
replace pct_sp4 = 0 if (revision_good_mean1<0 & revision_good_mean1>=-200 & !missing(fe4)) | (revision_bad_mean1<0 & revision_bad_mean1>=-200 & !missing(fe4)) | (revision_good_mean1>0 & revision_good_mean1<=200 & !missing(fe4))

gen pct_bn4 = .
replace pct_bn4 = 1 if (revision_good_mean1<0 & revision_good_mean1>=-200 & !missing(fe4))
replace pct_bn4 = 0 if (revision_bad_mean1>0 & revision_bad_mean1<=200 & !missing(fe4)) | (revision_good_mean1>0 & revision_good_mean1<=200 & !missing(fe4)) | (revision_bad_mean1<0 & revision_bad_mean1>=-200 & !missing(fe4))

tabstat revision_mean fe4 if pct_bp4 == 1 | pct_sn4 == 1, stats(mean median sd n)
tabstat revision_mean fe4 if pct_bn4 == 1 | pct_sp4 == 1, stats(mean median sd n)

tabstat revision_mean fe4 if pct_bp4 == 1, stats(mean median sd n)
tabstat revision_mean fe4 if pct_sp4 == 1, stats(mean median sd n)
tabstat revision_mean fe4 if pct_bn4 == 1, stats(mean median sd n)
tabstat revision_mean fe4 if pct_sn4 == 1, stats(mean median sd n)



*** ----------------------------------------------------------------------------
*** Table 2 --------------------------------------------------------------------
*** ----------------------------------------------------------------------------

*Prior-consistent
reghdfe fe3 revision_mean if (revision_good_mean1>0 & revision_good_mean1<=200) | (revision_bad_mean1<0 & revision_bad_mean1>=-200), absorb(anndats_year) vce(cluster anndats_year)
reghdfe fe4 revision_mean if (revision_good_mean1>0 & revision_good_mean1<=200) | (revision_bad_mean1<0 & revision_bad_mean1>=-200), absorb(anndats_year) vce(cluster anndats_year)
reghdfe fe5 revision_mean if (revision_good_mean1>0 & revision_good_mean1<=200) | (revision_bad_mean1<0 & revision_bad_mean1>=-200), absorb(anndats_year) vce(cluster anndats_year)

* Prior-inconsistent
reghdfe fe3 revision_mean if (revision_good_mean1<0 & revision_good_mean1>=-200) | (revision_bad_mean1>0 & revision_bad_mean1<=200), absorb(anndats_year) vce(cluster anndats_year)
reghdfe fe4 revision_mean if (revision_good_mean1<0 & revision_good_mean1>=-200) | (revision_bad_mean1>0 & revision_bad_mean1<=200), absorb(anndats_year) vce(cluster anndats_year)
reghdfe fe5 revision_mean if (revision_good_mean1<0 & revision_good_mean1>=-200) | (revision_bad_mean1>0 & revision_bad_mean1<=200), absorb(anndats_year) vce(cluster anndats_year)



*** ----------------------------------------------------------------------------
*** Table 7 --------------------------------------------------------------------
*** ----------------------------------------------------------------------------

* Buy recommendations, positive signal
reghdfe forecast_error3_good_mean1 revision_good_mean1 if revision_good_mean1>0 & revision_good_mean1<=200, absorb(anndats_year) vce(cluster anndats_year)
reghdfe forecast_error4_good_mean1 revision_good_mean1 if revision_good_mean1>0 & revision_good_mean1<=200, absorb(anndats_year) vce(cluster anndats_year)
reghdfe forecast_error5_good_mean1 revision_good_mean1 if revision_good_mean1>0 & revision_good_mean1<=200, absorb(anndats_year) vce(cluster anndats_year)

* Buy recommendation, negative signal
reghdfe forecast_error3_good_mean1 revision_good_mean1 if revision_good_mean1<0 & revision_good_mean1>=-200, absorb(anndats_year) vce(cluster anndats_year)
reghdfe forecast_error4_good_mean1 revision_good_mean1 if revision_good_mean1<0 & revision_good_mean1>=-200, absorb(anndats_year) vce(cluster anndats_year)
reghdfe forecast_error5_good_mean1 revision_good_mean1 if revision_good_mean1<0 & revision_good_mean1>=-200, absorb(anndats_year) vce(cluster anndats_year)


* Sell recommendations, positive signal
reghdfe forecast_error3_bad_mean1 revision_bad_mean1 if revision_bad_mean1>0 & revision_bad_mean1<=200, absorb(anndats_year) vce(cluster anndats_year)
reghdfe forecast_error4_bad_mean1 revision_bad_mean1 if revision_bad_mean1>0 & revision_bad_mean1<=200, absorb(anndats_year) vce(cluster anndats_year)
reghdfe forecast_error5_bad_mean1 revision_bad_mean1 if revision_bad_mean1>0 & revision_bad_mean1<=200, absorb(anndats_year) vce(cluster anndats_year)

* Sell recommendation, negative signal
reghdfe forecast_error3_bad_mean1 revision_bad_mean1 if revision_bad_mean1<0 & revision_bad_mean1>=-200, absorb(anndats_year) vce(cluster anndats_year)
reghdfe forecast_error4_bad_mean1 revision_bad_mean1 if revision_bad_mean1<0 & revision_bad_mean1>=-200, absorb(anndats_year) vce(cluster anndats_year)
reghdfe forecast_error5_bad_mean1 revision_bad_mean1 if revision_bad_mean1<0 & revision_bad_mean1>=-200, absorb(anndats_year) vce(cluster anndats_year)



*** ----------------------------------------------------------------------------
*** Table 3 --------------------------------------------------------------------
*** ----------------------------------------------------------------------------

use "$path\02_OutData\forecasts_act_ltg_cons.dta", clear 

*Change the structure for prior-consistent regs
expand 2 if !missing(revision_good_mean1) & !missing(revision_bad_mean1), gen(dup)
replace revision_good_mean1=. if !missing(revision_good_mean1) & !missing(revision_bad_mean1) & dup==0
replace revision_bad_mean1=. if !missing(revision_good_mean1) & !missing(revision_bad_mean1) & dup==1
forval k=3/5{
	replace forecast_error`k'_good_mean1=. if !missing(revision_good_mean1) & !missing(revision_bad_mean1) & dup==0
	replace forecast_error`k'_bad_mean1=. if !missing(revision_good_mean1) & !missing(revision_bad_mean1) & dup==1
}
*Prior-consistent 
forval k=3/5{
	gen fe`k'=forecast_error`k'_good_mean1
	replace fe`k'=forecast_error`k'_bad_mean1 if missing(fe`k')
}

gen revision_mean=revision_good_mean1
replace revision_mean=revision_bad_mean1 if missing(revision_mean)

destring anndats_year, replace

gen consistent =((!missing(revision_bad_mean1) & revision_mean < 0 & revision_mean >= -200) | (!missing(revision_good_mean1) & revision_mean > 0 & revision_mean <= 200))

gen inconsistent =((!missing(revision_bad_mean1) & revision_mean > 0 & revision_mean <= 200) | (!missing(revision_good_mean1) & revision_mean < 0 & revision_mean >= -200))

gen con_revision_mean=consistent*revision_mean

reghdfe fe4 consistent revision_mean con_revision_mean if (consistent==1 | inconsistent==1), absorb(anndats_year) vce(cluster anndats_year)

keep if (consistent==1 | inconsistent==1)

merge m:1 PERMNO anndats_year using "$path\02_OutData\uncertainty_data_CRSP.dta"
drop if _merge==2
drop _merge

egen MVE_quin = xtile(MVE), by(anndats_year) nq(5)
egen age_quin = xtile(age), by(anndats_year) nq(5)

reghdfe fe4 consistent revision_mean con_revision_mean if (consistent==1 | inconsistent==1), absorb(anndats_year) vce(cluster anndats_year)
reghdfe fe4 consistent revision_mean con_revision_mean if (consistent==1 | inconsistent==1) & MVE_quin==1, absorb(anndats_year) vce(cluster anndats_year)
reghdfe fe4 consistent revision_mean con_revision_mean if (consistent==1 | inconsistent==1) & MVE_quin==5, absorb(anndats_year) vce(cluster anndats_year)
reghdfe fe4 consistent revision_mean con_revision_mean if (consistent==1 | inconsistent==1) & age_quin==1, absorb(anndats_year) vce(cluster anndats_year)
reghdfe fe4 consistent revision_mean con_revision_mean if (consistent==1 | inconsistent==1) & age_quin==5, absorb(anndats_year) vce(cluster anndats_year)



*** ----------------------------------------------------------------------------
*** Table 1 - individual -------------------------------------------------------
*** ----------------------------------------------------------------------------

*** 1 --------------------------------------------------------------------------
use "$path\02_OutData\forecasts_act_ltg_ind.dta", clear

keep if (revision >= -200 & revision < 0 & bad == 1) | (revision > 0 & revision <= 200 & good == 1) | (revision >= -200 & revision < 0 & good == 1) | (revision > 0 & revision <= 200 & bad == 1)

keep if !missing(forecast_error4_)
bys ANALYS: egen tot_for=count(revision)
keep if tot_for>=10

* Number of revisions in analyst-firm-year
bys TICKER anndats_year ANALYS: egen count_revision = count(revision)
bys TICKER anndats_year ANALYS: gen dup=cond(_N==1,0,_n)
keep if dup<2
drop dup

tabstat count_revision, stats(mean median sd n)


*** 2 --------------------------------------------------------------------------
use "$path\02_OutData\forecasts_act_ltg_ind.dta", clear

keep if (revision >= -200 & revision < 0 & bad == 1) | (revision > 0 & revision <= 200 & good == 1) | (revision >= -200 & revision < 0 & good == 1) | (revision > 0 & revision <= 200 & bad == 1)

* prior-consistent
keep if (revision >= -200 & revision < 0 & bad == 1) | (revision > 0 & revision <= 200 & good == 1)

keep if !missing(forecast_error4_)
bys ANALYS: egen tot_for=count(revision)
keep if tot_for>=10

tabstat revision forecast_error4_ if !missing(forecast_error4_), stats(mean median sd n)


*** 3 --------------------------------------------------------------------------
use "$path\02_OutData\forecasts_act_ltg_ind.dta", clear

keep if (revision >= -200 & revision < 0 & bad == 1) | (revision > 0 & revision <= 200 & good == 1) | (revision >= -200 & revision < 0 & good == 1) | (revision > 0 & revision <= 200 & bad == 1)

* prior-consistent
keep if (revision >= -200 & revision < 0 & good == 1) | (revision > 0 & revision <= 200 & bad == 1)

keep if !missing(forecast_error4_)
bys ANALYS: egen tot_for=count(revision)
keep if tot_for>=10

tabstat revision forecast_error4_ if !missing(forecast_error4_), stats(mean median sd n)



*** ----------------------------------------------------------------------------
*** Table 4 Panel A ------------------------------------------------------------
*** ----------------------------------------------------------------------------

use "$path\02_OutData\forecasts_act_ltg_ind.dta", clear

gen consistent =((bad == 1 & revision < 0 & revision >= -200) | (good == 1 & revision > 0 & revision <= 200))

gen inconsistent =((bad == 1 & revision > 0 & revision <= 200) | (good == 1 & revision < 0 & revision >= -200))
gen con_revision=consistent*revision

drop if revision<-200 | revision>200
forval k=3/5{
reghdfe forecast_error`k'_ con_revision revision consistent if consistent==1 | inconsistent==1, absorb(TICKER anndats_year) vce(cluster ANALYS)
lincom con_revision + revision
}



*** ----------------------------------------------------------------------------
*** Table 4 Panel B ------------------------------------------------------------
*** ----------------------------------------------------------------------------

*** prior-consistent -----------------------------------------------------------
use "$path\02_OutData\forecasts_act_ltg_ind.dta", clear

* Run the same code for years 4 and 5 just change the number in forecast_error3_ to forecast_error4_ or forecast_error5_
keep if (bad==1 & revision<0 & revision>=-200) | (good==1 & revision>0 & revision<=200)

keep if !missing(forecast_error3_)
bys ANALYS: egen tot_for=count(revision)
keep if tot_for>=10

* just a dummy dataset since statsby requires an unaltered dataset
save "$path\more_10.dta", replace

statsby, by(ANALYS): reg forecast_error3_ revision 
qreg _b_revision

drop if missing(_b_revision)
gen group = 1

save "$path\02_OutData\betas_pc_ind_analysts.dta", replace


*** prior-inconsistent ---------------------------------------------------------
use "$path\02_OutData\forecasts_act_ltg_ind.dta", clear

* Run the same code for years 4 and 5 just change the number in forecast_error3_ to forecast_error4_ or forecast_error5_
keep if (bad==1 & revision>0 & revision<=200) | (good==1 & revision<0 & revision>=-200)

keep if !missing(forecast_error3_)
bys ANALYS: egen tot_for=count(revision)
keep if tot_for>=10

* just a dummy dataset since statsby requires an unaltered dataset
save "$path\more_10.dta", replace

statsby, by(ANALYS): reg forecast_error3_ revision 
qreg _b_revision

drop if missing(_b_revision)
gen group = 0

append using "$path\02_OutData\betas_pc_ind_analysts.dta"
qreg _b_revision group, vce(robust)



*** ----------------------------------------------------------------------------
*** Table 5 --------------------------------------------------------------------
*** ----------------------------------------------------------------------------

use "$path\02_OutData\forecasts_act_ltg_ind_eavars.dta", clear

tostring PENDS, replace
gen PENDS_year  = substr(PENDS, 1, 4)
gen PENDS_month = substr(PENDS, 5, 2)
gen PENDS_day   = substr(PENDS, 7, 2)
destring PENDS_year PENDS_month PENDS_day, replace

merge m:1 TICKER ANALYS PENDS_year PENDS_month PENDS_day using "$path\02_OutData\quarterly_surprises.dta"
keep if _merge == 3
drop _merge

gen permno = PERMNO

tostring PENDS, replace
gen pends_year  = substr(PENDS, 1, 4)
gen pends_month = substr(PENDS, 5, 2)

destring pends_year, replace
destring pends_month, replace

*replace pends_year = pends_year - 1

merge m:1 permno pends_year pends_month using "$path\02_OutData\ccm_data_quarterly_to_merge.dta"
keep if _merge == 3
drop _merge

sort ANALYS TICKER anndats_revision
bys ANALYS TICKER: gen l_anndats_revision = anndats_revision[_n - 1]
bys ANALYS TICKER: gen l_fcst_value       = fcst_value[_n - 1]

format l_anndats_revision %d

**** filtering ****
gen ea_revision1 = anndats_revision - l1_rdq
gen ea_revision2 = anndats_revision - l2_rdq
gen ea_revision3 = anndats_revision - l3_rdq
gen ea_revision4 = anndats_revision - l4_rdq

gen last_ea_revision1 = l_anndats_revision - l1_rdq
gen last_ea_revision2 = l_anndats_revision - l2_rdq
gen last_ea_revision3 = l_anndats_revision - l3_rdq
gen last_ea_revision4 = l_anndats_revision - l4_rdq

*format rdq %d
*keep ANALYS TICKER PENDS anndats_revision rdq l1_rdq l2_rdq l3_rdq l4_rdq ea_revision last_ea_revision
*sort TICKER ANALYS anndats_revision

keep if ((ea_revision1 > 0 & ea_revision1 <= 20 & last_ea_revision1 < 0 & last_ea_revision1 >= -90) | ///
         (ea_revision2 > 0 & ea_revision2 <= 20 & last_ea_revision2 < 0 & last_ea_revision2 >= -90) | ///
         (ea_revision3 > 0 & ea_revision3 <= 20 & last_ea_revision3 < 0 & last_ea_revision3 >= -90) | ///
         (ea_revision4 > 0 & ea_revision4 <= 20 & last_ea_revision4 < 0 & last_ea_revision4 >= -90)) & ///
         !missing(l_fcst_value)

gen consistent =((bad == 1 & revision < 0 & revision >= -200 & quart_eps_fcast_error < 0) | (good == 1 & revision > 0 & revision <= 200 & quart_eps_fcast_error > 0))

gen inconsistent =((bad == 1 & revision > 0 & quart_eps_fcast_error > 0) | (good == 1 & revision < 0 & quart_eps_fcast_error < 0))
gen con_revision=consistent*revision

drop if revision<-200 | revision>200
forval k=3/5{
	reghdfe forecast_error`k'_ con_revision revision consistent if consistent==1 | inconsistent==1, absorb(TICKER anndats_year) vce(cluster ANALYS)
}



*** ----------------------------------------------------------------------------
*** Table 6 --------------------------------------------------------------------
*** ----------------------------------------------------------------------------

use "$path\02_OutData\forecasts_act_ltg_ind_unaff.dta", clear

gen consistent =((bad == 1 & revision < 0 & revision >= -200) | (good == 1 & revision > 0 & revision <= 200))

gen inconsistent =((bad == 1 & revision > 0 & revision <= 200) | (good == 1 & revision < 0 & revision >= -200))
gen con_revision=consistent*revision

drop if revision<-200 | revision>200
forval k=3/5{
reghdfe forecast_error`k'_ con_revision revision consistent if consistent==1 | inconsistent==1, absorb(TICKER anndats_year) vce(cluster ANALYS)
lincom con_revision + revision
}



*** ----------------------------------------------------------------------------
*** Table 8 --------------------------------------------------------------------
*** ----------------------------------------------------------------------------

* positive signal --------------------------------------------------------------
use "$path\02_OutData\forecasts_act_ltg_ind.dta", clear

gen consistent = (good == 1 & revision > 0 & revision <= 200)

gen inconsistent = (bad == 1 & revision > 0 & revision <= 200)
gen con_revision = consistent*revision

drop if revision<-200 | revision>200
forval k=3/5{
reghdfe forecast_error`k'_ con_revision revision consistent if consistent==1 | inconsistent==1, absorb(TICKER anndats_year) vce(cluster ANALYS)
lincom con_revision + revision
}


* negative signal --------------------------------------------------------------
use "$path\02_OutData\forecasts_act_ltg_ind.dta", clear

gen consistent = (bad == 1 & revision < 0 & revision >= -200)

gen inconsistent = (good == 1 & revision < 0 & revision >= -200)
gen con_revision = consistent*revision

drop if revision<-200 | revision>200
forval k=3/5{
reghdfe forecast_error`k'_ con_revision revision consistent if consistent==1 | inconsistent==1, absorb(TICKER anndats_year) vce(cluster ANALYS)
lincom con_revision + revision
}


